package com.zhp.farmerclient.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhp.farmerclient.common.Result;
import com.zhp.farmerclient.entity.Comment;
import com.zhp.farmerclient.entity.Post;
import com.zhp.farmerclient.entity.User;
import com.zhp.farmerclient.mapper.CommentMapper;
import com.zhp.farmerclient.mapper.PostMapper;
import com.zhp.farmerclient.mapper.UserMapper;
import com.zhp.farmerclient.utils.JwtUtils;
import com.zhp.farmerclient.utils.ShiroUtils;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.List;

@Service
public class PostService {
    @Autowired
    public PostMapper postMapper;

    @Autowired
    public CommentMapper commentMapper;

    @Autowired
    public UserMapper userMapper;

    @Autowired
    public JwtUtils jwtUtils;

    //分页
    public Result list(Integer page,Integer size){
       Page<Post> p = new Page<>(page,size);
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        IPage<Post> iPage = postMapper.selectPage(p,queryWrapper);
        return Result.succ(iPage.getRecords(),iPage.getTotal());
    }
    //部分显示
    public Result part(){
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        List<Post> post = postMapper.selectList(queryWrapper);
        return Result.succ(post,null);
    }
    //详情页
    public Result detail(int id){
       Post post = postMapper.selectById(id);
//        Assert.notNull(post,"该讨论已被删除");
       return Result.succ(post,null);
    }
    //点赞
    public  Result likes(int id){
        Post post = postMapper.selectById(id);
        post.setLikes(post.getLikes()+1);
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        postMapper.update(post,queryWrapper.eq("id",id));
        return Result.succ();
    }
    //编辑
    public Result edit(Post post){
        postMapper.insert(post);
        return Result.succ();
    }

    //根据用户ID查看用户帖子
    public Result check(Integer page, Integer size, Integer id) {
        Page<Post> p = new Page<>(page,size);
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        queryWrapper.eq("user_id",id);
        IPage<Post> iPage = postMapper.selectPage(p,queryWrapper);
        return Result.succ(iPage.getRecords(),iPage.getTotal());
    }

    //根据帖子ID删除帖子
    public Result delete(int id) {
        //根据帖子ID删除与之相关的评论
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("post_id",id);
        commentMapper.delete(commentQueryWrapper);

        postMapper.deleteById(id);
        return Result.succ();
    }
    //批量删除
    public Result deletes(List<Integer> ids) {

        for (Integer i:ids){
            //根据帖子ID删除与之相关的评论
            QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
            commentQueryWrapper.eq("post_id",i);
            commentMapper.delete(commentQueryWrapper);
        }


        postMapper.deleteBatchIds(ids);
        return Result.succ();
    }

    //分页显示（查询）
    public Result page(Integer pageNum, Integer pageSize, String title, String content, String user_id) {
        Page<Post> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        if (!"".equals(title)) {
            queryWrapper.like("title", title);
        }
        if (!"".equals(content)) {
            queryWrapper.like("content", content);
        }
        if (!"".equals(user_id)) {
            queryWrapper.like("user_id", user_id);
        }
        queryWrapper.orderByDesc("id");
        IPage<Post> iPage = postMapper.selectPage(page,queryWrapper);
        return Result.succ(iPage.getRecords(),iPage.getTotal());
    }

}
